/**
 * 
 */
package de.fsujena.chat_pi.client;

import java.io.BufferedReader;
import java.io.Console;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.prefs.Preferences;

import de.fsujena.chat_pi.client.userInterface.ConsoleUI;
import de.fsujena.chat_pi.client.userInterface.GraphicalUI;
import de.fsujena.chat_pi.client.userInterface.UserInterface;

import de.fsujena.chat_pi.ressources.Room;
import de.fsujena.chat_pi.ressources.User;
import de.fsujena.chat_pi.ressources.message.TextMessage;

/**
 * @author erik
 * 
 */
public class Client {
	
	public ConnectionThread connectionThread = null; 	// Thread that handels the connection to the server
	public Boolean isconnected = false; 				// connectionstate of the client
	public String ipadress = null;						// the ipadress of the server
	public int port = 0; 								// the port on which the server runs
	public String newusername; 							// new password (after change)
	public String newpassword; 							// new username (after change)
	public Worker worker;
	public UserInterface UI;
	public List<String> msgArray=new ArrayList<String>();
	User nullUser=null;
	public Room foyer=new Room("Foyer",null,0,nullUser);
	public Room room;
	public List<String> roomDir =new ArrayList<String>();
	String username; 									// the username with wich the user is logged in
	String password; 									// the password of the user //TODO encrypt this
														// Console console = System.console();
	Console console = null; 							// reference to the system console(not realy needed)
	BufferedReader input = new BufferedReader(new InputStreamReader(System.in)); // reads user input
	public Preferences prefs;

	/**
	 * @param args
	 */
	/**
	 * starts the client
	 * 
	 * @param args
	 *            unused
	 */
	public static void main(String[] args) {
		new Client().startClient(args);
	}

	/**
	 * reads the input and processes it
	 */
	void startClient(String[] args) {
		// load preferences
		prefs = Preferences.userNodeForPackage(this.getClass());

		worker = new Worker(this);
		if (args.length>0 && args[0].equals("console")){
		UI = new ConsoleUI(this);
		}else{		
		UI = new GraphicalUI(this);
		}
			new Thread(UI).start();
			
			
	}

	/**
	 * stops the programm
	 */
	public void killClient() {
		if (isconnected)
			worker.disconnect(); 		// disconnect from the server
		System.exit(0); 				// stop the main thread
		return;
	}
	
	public int getPort() {
		return prefs.getInt("port", 3141);
	}
	
	public void setPort(int port) {
		prefs.putInt("port", port);
	}

	public String getIP() {
		return prefs.get("IPaddress", "localhost");
	}
	
	public void setIP(String ip) {
		prefs.put("IPaddress", ip);
	}

	public String getUsername() {
		return prefs.get("username", "");
	}
	
	public void setUsername(String username) {
		prefs.put("username", username);
	}

	public String getPassword() {
		return prefs.get("password", "");
	}
	
	public void setPassword(String password) {
		prefs.put("password", password);
	}
	public void setFoyer(Room Foyer){
		
		this.foyer=Foyer.copyRoom();
	}
}
